﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using io = System.IO;
using System.IO.IsolatedStorage;
using System.Reflection;

namespace FolderHider
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        InteropINVars _ini;
        string _location;

        public MainWindow()
        {
            InitializeComponent();
            INI();
        }
        private void INI()
        {
            _ini = new InteropINVars(IsolatedStorageFile.GetUserStoreForAssembly());
            _location = io.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            btn_main.Click += delegate { StartAction(); };
            btn_main.ToolTip = "Execute command";

            txt_name.KeyDown += new KeyEventHandler(txt_name_KeyDown);
            txt_name.Focus();
        }

        private void txt_name_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                StartAction();
                return;
            }
        }

        /// <summary>
        /// Function that call the correct command
        /// </summary>
        private void StartAction()
        {
            txt_results.Text = "";


            if (txt_name.Text == "!Help")
            {
                DoHelpCMD();
                return;
            }

            List<string> lista = GetData(txt_name.Text);
            if (lista == null || lista.Count <= 1)
            {
                txt_results.Text = "Invalid command,\nType !Help for standard commandlist";
                return;
            }

            if (_ini.GetCommandIndex(lista[0]) == 0 && lista.Count == 2)
                DoHelpCMD(lista[1]);
            else if (_ini.GetCommandIndex(lista[0]) == 1 && lista.Count == 2)
                ShowFolders(lista[1]);
            else if (_ini.GetCommandIndex(lista[0]) == 2 && lista.Count == 3)
                ChangePass(lista[1], lista[2]);
            else if (_ini.GetCommandIndex(lista[0]) == 3 && lista.Count == 3)
                ShowHide(lista[1], lista[2]);
            else if (_ini.GetCommandIndex(lista[0]) == 4 && lista.Count == 4)
                ChangeCMD(lista[1], lista[2], lista[3]);
            else
                txt_results.Text = "Invalid command,\nType !Help for standard commandlist";
        }

        /// <summary>
        /// Execute standard help command
        /// </summary>
        private void DoHelpCMD()
        {
            txt_results.Text = "!Help - Standard commands \n!Help*[PASSWORD]# - Current commands \n!ShowList*[PASSWORD]# - Show all folders name" +
                "\n!ChangeKP*[PASSWORD]*[NEWPASSWORD]# - Change Current password \n!FH*[FOLDERNAME]*[PASSWORD]# - Show\\Hide folder" +
                "\n!ChgCMD*[OLDCMD]*[NEWCMD]*[PASSWORD]# - Change commands function";
        }
        /// <summary>
        /// Execute command [0]
        /// </summary>
        /// <param name="arg1">Password</param>
        private void DoHelpCMD(string arg1)
        {
            if (_ini.KEY != arg1)
            {
                txt_results.Text = "Password is invalid";
                return;
            }

            txt_results.Text = "!Help - Standard commands \n!" + _ini.GetCommand(0) + "*[PASSWORD]# - Current commands \n!" + 
                _ini.GetCommand(1) + "*[PASSWORD]# - Show all folders name" +
                "\n!" + _ini.GetCommand(2) + "*[PASSWORD]*[NEWPASSWORD]# - Change Current password \n!" + 
                _ini.GetCommand(3) + "*[FOLDERNAME]*[PASSWORD]# - Show\\Hide folder" +
                "\n!" + _ini.GetCommand(4) + "*[OLDCMD]*[NEWCMD]*[PASSWORD]# - Change commands function";
        }
        /// <summary>
        /// Execute command [1]
        /// </summary>
        /// <param name="arg1">password</param>
        private void ShowFolders(string arg1)
        {
            if (_ini.KEY != arg1)
            {
                txt_results.Text = "Password is invalid";
                return;
            }

            txt_results.Text = "Nº Folders: " + _ini.Folders.Count + "\n\n";

            foreach (string p in _ini.Folders)
                txt_results.Text += p + "\n";
        }
        /// <summary>
        /// Execute command [2]
        /// </summary>
        /// <param name="arg1">Old Password</param>
        /// <param name="arg2">New Password</param>
        private void ChangePass(string arg1, string arg2)
        {
            if (_ini.KEY != arg1)
            {
                txt_results.Text = "Password is invalid";
                return;
            }

            _ini.ChangePass(arg2);
            txt_results.Text = "Password changed to " + arg2;
        }
        /// <summary>
        /// Execute command [3]
        /// </summary>
        /// <param name="arg1">Folder to Hide</param>
        /// <param name="arg2">Password</param>
        private void ShowHide(string arg1, string arg2)
        {
            if (_ini.KEY != arg2)
            {
                txt_results.Text = "Password is invalid";
                return;
            }

            string pth = io.Path.Combine(_location, arg1);

            if (!io.Directory.Exists(pth))
            {
                txt_results.Text = "Invalid folder";
                return;
            }

            io.DirectoryInfo info = new io.DirectoryInfo(pth);
            string tmp = info.Attributes.ToString();

            if (tmp.IndexOf("Hidden") != -1 && tmp.IndexOf("System") != -1)
            {
                info.Attributes &= ~io.FileAttributes.Hidden;
                info.Attributes &= ~io.FileAttributes.System;
                _ini.RemoveFolder(arg1);
                txt_results.Text = arg1 +  " is unHidden";
            }
            else
            {
                info.Attributes |= io.FileAttributes.Hidden;
                info.Attributes |= io.FileAttributes.System;
                _ini.AddFolder(arg1);
                txt_results.Text = arg1 + " is Hidden";
            }
        }
        /// <summary>
        /// Execute command [4]
        /// </summary>
        /// <param name="arg1">Old Command string</param>
        /// <param name="arg2">New Command string</param>
        /// <param name="arg3">Password</param>
        private void ChangeCMD(string arg1, string arg2, string arg3)
        {
            if (_ini.KEY != arg3)
            {
                txt_results.Text = "Password is invalid";
                return;
            }

            int i = _ini.GetCommandIndex(arg1);
            if (i == -1)
            {
                txt_results.Text = arg1 + " is a invalid command name";
                return;
            }

            _ini.SetCommand(arg2, i);
            txt_results.Text = arg1 + " command changed to " + arg2;
        }

        /// <summary>
        /// Parse arguments from command
        /// </summary>
        /// <param name="data">command</param>
        /// <returns></returns>
        private List<string> GetData(string data)
        {
            List<string> list = new List<string>();


            if (String.IsNullOrEmpty(data)) return null;
            if (data[0] != '!') return null;
            if (data.IndexOf("*") == -1) return null;
            if (data[data.Length - 1] != '#') return null;
            if (data.IndexOf("*") < 2) return null;
            if (((data.Length - 1) - data.LastIndexOf("*")) < 2) return null;

            data = data.Substring(1);
            list.Add(data.Substring(0, data.IndexOf("*")));
            data = data.Substring(data.IndexOf("*") + 1);

            while (true)
            {
                int x = data.IndexOf("*");
                if (x == -1) x = data.IndexOf("#");

                list.Add(data.Substring(0, x));

                if (x == data.Length - 1) break;
                x++;
                data = data.Substring(x);
            }

            return list;
        }
    }
}
